<?php
declare(strict_types=1);

namespace app\common\model\card;

use think\Model;

/**
 * 卡片限额重试记录模型
 * Class CardLimitRetry
 * @package app\common\model\card
 */
class CardLimitRetry extends Model
{
    protected $name = 'card_limit_retry';
    
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    
    // 状态常量
    const STATUS_PENDING = 0;    // 待处理
    const STATUS_SUCCESS = 1;    // 处理成功
    const STATUS_FAILED = 2;     // 处理失败
    
    // 默认值
    protected $default = [
        'status' => 0,
        'retry_count' => 0
    ];
    
    /**
     * 添加重试记录
     * @param array $data 重试数据
     * @return bool
     */
    public static function addRetryRecord(array $data): bool
    {
        try {
            $model = new self();
            $model->save([
                'card_id' => $data['card_id'],
                'card_bin' => $data['card_bin'],
                'single_transaction_limit' => $data['single_transaction_limit'] ?? 0,
                'daily_transaction_limit' => $data['daily_transaction_limit'] ?? 0,
                'weekly_transaction_limit' => $data['weekly_transaction_limit'] ?? 0,
                'monthly_transaction_limit' => $data['monthly_transaction_limit'] ?? 0,
                'yearly_transaction_limit' => $data['yearly_transaction_limit'] ?? 0,
                'total_transaction_limit' => $data['total_transaction_limit'] ?? 0,
                'error_message' => $data['error_message'] ?? '',
                'status' => 0,
                'retry_count' => 0
            ]);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
    
    /**
     * 更新重试状态
     * @param int $id 记录ID
     * @param bool $success 是否成功
     * @param string $message 结果消息
     * @return bool
     */
    public static function updateRetryStatus(int $id, bool $success, string $message = ''): bool
    {
        try {
            $model = self::find($id);
            if (!$model) {
                return false;
            }

            $model->save([
                'status' => $success ? 1 : 2,
                'retry_count' => $model->retry_count + 1,
                'result_message' => $message
            ]);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
    
    /**
     * 获取待重试的记录
     * @param int $limit 限制数量
     * @return array
     */
    public static function getPendingRetries(int $limit = 10): array
    {
        return self::where('status', 0)
            ->where('retry_count', '<', 3)
            ->order('create_time', 'asc')
            ->limit($limit)
            ->select()
            ->toArray();
    }

    /**
     * 获取重试记录详情
     * @param int $id 记录ID
     * @return array|null
     */
    public static function getRetryDetail(int $id): ?array
    {
        $record = self::find($id);
        return $record ? $record->toArray() : null;
    }

    /**
     * 获取卡片的所有重试记录
     * @param string $cardId 卡片ID
     * @return array
     */
    public static function getCardRetries(string $cardId): array
    {
        return self::where('card_id', $cardId)
            ->order('create_time', 'desc')
            ->select()
            ->toArray();
    }
} 